local skynet = require "skynet"
require "skynet.manager"
local Module = require "core.Module"
local ServiceName = require "enum.ServiceName"

Module.init()

skynet.start(function()
    skynet.monitor("service/Monitor")
    skynet.uniqueservice("debug_console", skynet.getenv("debug_ip"), skynet.getenvn("debug_port"))
    skynet.newservice("service/HotMgr")

    skynet.new_hotservice("service.Logger")
    skynet.new_hotservice("service.EventMgr")

    local mongoPool = skynet.new_hotservice("service.MongoPool")
    skynet.call(mongoPool, "lua", "start", {
        host = skynet.getenv("mongo_host"),
        port = skynet.getenvn("mongo_port"),
        username = skynet.getenv("mongo_username"),
        password = skynet.getenv("mongo_password"),
        db = skynet.getenv("mongo_db"),
        flush_interval = skynet.getenvn("mongo_flush_interval"),
        flush_count = skynet.getenvn("mongo_flush_count")
    })

    local wsgate = skynet.new_hotservice("service.GateMaster")
    skynet.call(wsgate, "lua", "start", {
        host = skynet.getenv("server_host"),
        port = skynet.getenvn("server_port"),
        protocol = skynet.getenv("server_protol"),
        maxclient = skynet.getenvn("maxclient"),
        gatecount = skynet.getenvn("gatecount")
    })

    local apigate = skynet.new_hotservice("service.ApiMaster")
    skynet.call(apigate, "lua", "start", {
        protocol = skynet.getenv("api_protocol"),
        host = skynet.getenv("api_host") or "0.0.0.0",
        port = skynet.getenvn("api_port"),
        gatecount = skynet.getenvn("api_gatecount"),
        admin_host = skynet.getenv("admin_host")
    })

    -- 初始化模块的唯一服务(带名字的服务)
    for _, mod in pairs(Module.allServiceMod()) do
        local servicePath = Module.servicePath(mod, true)
        local modService = skynet.new_hotservice(servicePath)
        skynet.name(ServiceName.getModuleName(mod), modService)
    end

    for _, mod in pairs(Module.allServiceMod()) do
        skynet.call(ServiceName.getModuleName(mod), "lua", "start")
    end

    skynet.exit()
end)
